package com.xinxin.blockchain.api.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.Collections;

@Configuration
@EnableSwagger2
public class Swagger2Config {

    @Value("${swagger.enable:false}")
    private boolean enable;

    @Bean
    public Docket createRestApi() {
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .enable(enable)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.xinxin.blockchain.api.controller"))
                .paths(PathSelectors.any())
                .build();

        // 添加header参数
        ParameterBuilder parameterBuilder = new ParameterBuilder();
        parameterBuilder.parameterType("header")
                .name("key").description("令牌").required(false)
                .modelRef(new ModelRef("string"));
        docket.globalOperationParameters(Collections.singletonList(parameterBuilder.build()));

        return docket;
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Api接口文档")
                .description("Spring Boot Swagger2 构建RESTful API")
                .version("1.0")
                .contact(new Contact("blockchain", "https://www.blockchain.com", "blockchain@gmail.com"))
                .license("Apache 2.0")
                .licenseUrl("https://www.apache.org/licenses/LICENSE-2.0.html")
                .termsOfServiceUrl("https://www.blockchain.com/blockchain/doc.html")
                .build();
    }
}
